﻿/*
 * Copyright Mark Dawson 2008 - http://www.markdawson.org/kit3D
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Kit3D.Windows.Controls;
using Kit3D.Windows.Media;
using Kit3D.Windows.Media.Media3D;
using System.Windows.Media.Imaging;

namespace TestApp
{
    public partial class TigerSolid : UserControl
    {
        private ModelVisual3D tigerVisual;
        private ModelVisual3D tigerVisual2;
        private Viewport3D viewport;

        public TigerSolid()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DrawTiger();

            Kit3D.Windows.Media.CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        double rotation = 0;
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (tigerVisual != null)
            {
                Transform3DGroup tg = new Transform3DGroup();

                tg.Children.Add(new TranslateTransform3D(0, 0, -1));
                tg.Children.Add(new RotateTransform3D(
                                    new AxisAngleRotation3D(
                                        new Vector3D(0 ,10 ,0), 
                                        rotation), 
                                        new Point3D(0,0,0)));

                tigerVisual2.Transform = tg;

                tg = new Transform3DGroup();
                tg.Children.Add(new RotateTransform3D(
                    new AxisAngleRotation3D(
                        new Vector3D(0, 10, 0),
                        rotation),
                        new Point3D(0, 0, 0)));
                tigerVisual.Transform = tg;
            }

            rotation += 3;
        }

        private void DrawTiger()
        {
            //Store reference to the visual so we can animate it later
            this.tigerVisual = CreateTigerVisual(Colors.Red);

            this.tigerVisual2 = CreateTigerVisual(Colors.Green);
            this.tigerVisual2.Transform = new TranslateTransform3D(0, 0, -1);
            this.tigerVisual.ZIndexOverride = 1;

            //Need to create a viewport and camera to be able to view the scene
            viewport = new Viewport3D();

            //Important to give the viewport either a fixed width and height or stretch it,
            //it does not grown to fill its content, so if you don't do this then you will
            //not see anything.
            viewport.HorizontalAlignment = HorizontalAlignment.Stretch;
            viewport.VerticalAlignment = VerticalAlignment.Stretch;
            viewport.Camera = new PerspectiveCamera(new Point3D(0, 0, 7),
                                        new Vector3D(0, 0, -1),
                                        new Vector3D(0, 1, 0),
                                        45);

            //Have to add the visual to the viewport and finally add the viewport
            //to the main XAML page otherwise it will not be rendered.
            viewport.Children.Add(this.tigerVisual);
            //viewport.Children.Add(this.tigerVisual2);
            this.LayoutRoot.Children.Add(viewport);
        }

        private ModelVisual3D CreateTigerVisual(Color fillColor)
        {
            string positions = "0,-0.061426,0.9388459 -0.049693,0.200773,0.692165 0,-0.031454,1.28361 0.049693,-0.023019,0.9505299 0.032306,0.045667,0.9714239 0,0.05328,0.9737409 -0.032306,0.269459,0.7130589 -0.023241,0.009164999,1.5051 0,0.277072,0.715376 0,0.014811,1.50415 0.049693,-0.111196,1.28231 0.032306,-0.039411,1.28348 0.023242,0.009164999,1.5051 0.035751,-0.041767,1.51368 0.032306,0.269459,0.7130589 0.049693,0.200773,0.692165 0,0.162366,0.680481 -0.032306,0.045667,0.9714239 -0.049693,-0.023019,0.9505299 0,-0.151335,1.28166 0,-0.070246,1.51848 -0.049693,-0.111196,1.28231 -0.03575,-0.041767,1.51368 -0.032306,-0.039411,1.28348 0,-0.139499,-0.8041489 0,-0.196023,-0.6439139 0,0.207794,-1.1627 0,0.342375,-1.51026 0,-0.232522,-0.216349 0.109118,-0.119018,0.100709 -0.109118,-0.119018,0.100709 0,-0.154873,0.103656 0.030101,-0.105174,0.316078 0,-0.112314,0.317509 0,0.112303,0.5060779 -0.030101,-0.105174,0.316078 -0.028761,-0.037815,0.439801 0.028761,-0.037815,0.439801 0.08173899,-0.203143,0.439706 0.06593599,-0.238012,0.335711 0.020142,-0.108242,0.303668 0.114666,-0.167257,0.149229 0.116086,-0.276934,0.217803 0.09003399,-0.382159,0.456303 -0.114666,-0.167257,0.149229 -0.116086,-0.276934,0.217803 -0.020142,-0.108242,0.303668 -0.09003399,-0.382159,0.456303 -0.08173899,-0.203143,0.439706 -0.076865,-0.377381,0.378108 -0.06593599,-0.238012,0.335711 0.076865,-0.377381,0.378108 -0.077999,-0.438418,0.363464 -0.09472499,-0.6879839,0.346619 -0.11979,-0.424686,0.304048 -0.16082,-0.6026589,0.286432 -0.190992,-0.614722,0.342338 -0.168141,-0.6877069,0.257272 -0.151771,-0.6879799,0.349147 -0.203745,-0.6879169,0.313177 -0.1285,-0.6874449,0.237457 -0.07918599,-0.687932,0.310966 -0.165267,-0.6022909,0.204563 -0.07631199,-0.6170989,0.236559 -0.125626,-0.603366,0.203812 -0.127225,-0.603735,0.285509 -0.08543399,-0.617617,0.340127 -0.06893899,-0.6874139,0.212852 -0.118657,-0.370966,0.289767 -0.153385,-0.423611,0.30541 -0.215919,-0.687399,0.214649 -0.20087,-0.6144719,0.257455 -0.204332,-0.687587,0.149699 -0.191896,-0.6462079,0.173407 -0.162472,-0.639473,0.139614 -0.173907,-0.6873379,0.180856 -0.169612,-0.6875529,0.115905 -0.12971,-0.6400809,0.138863 -0.12713,-0.687344,0.180105 -0.130953,-0.687556,0.115154 -0.08895499,-0.647844,0.17161 -0.08286199,-0.687595,0.147901 -0.091167,-0.448646,0.416058 -0.146947,-0.6269749,0.378308 -0.09860199,-0.6278449,0.375781 -0.183557,-0.435523,0.366725 -0.139512,-0.447776,0.419786 -0.036911,-0.131458,-0.8007739 -0.114132,-0.130986,-0.8937849 -0.218544,-0.6578469,-0.9026449 -0.171757,-0.5324879,-0.8961189 -0.07399599,0.255722,-1.48352 -0.05122999,0.326389,-1.50415 -0.06501099,0.221538,-1.46405 0.104586,-0.228675,-0.6927029 0.091167,-0.448646,0.416057 0.077999,-0.438418,0.363464 0.098603,-0.6278449,0.375781 0.09472499,-0.6879839,0.346619 0.11979,-0.424686,0.304048 0.165267,-0.6022909,0.204563 0.08543399,-0.617617,0.340127 0.127225,-0.603735,0.285508 0.125626,-0.603366,0.203812 0.07631199,-0.6170989,0.236559 0.118657,-0.370966,0.289767 0.204332,-0.687587,0.149699 0.162472,-0.639473,0.139614 0.191896,-0.6462079,0.173407 0.169612,-0.6875529,0.115905 0.12971,-0.6400809,0.138863 0.06893899,-0.6874139,0.212852 0.12713,-0.687344,0.180104 0.08286199,-0.687595,0.147901 0.08895499,-0.647844,0.17161 0.130953,-0.687556,0.115154 0.153385,-0.423611,0.30541 0.16082,-0.6026589,0.286432 0.168141,-0.6877069,0.257271 0.1285,-0.6874449,0.237457 0.07918599,-0.687932,0.310966 0.203745,-0.6879169,0.313177 0.173907,-0.6873379,0.180856 0.215919,-0.687399,0.214649 0.20087,-0.6144719,0.257455 0.190992,-0.614722,0.342338 0.146947,-0.6269749,0.378308 0.151771,-0.6879789,0.349147 0.183557,-0.435523,0.366725 0.139512,-0.447776,0.419786 0.113246,-0.5583349,-0.7520499 -0.113029,-0.5583349,-0.7520499 -0.058263,-0.180258,-0.7923719 -0.104586,-0.228675,-0.6927029 0.058263,-0.180258,-0.7923719 0.114132,-0.130986,-0.8937849 0.036911,-0.131458,-0.8007739 0,-0.004468999,-0.968083 0,0.159119,-1.08089 0.055861,-0.654043,-0.9072679 0.079285,-0.5450549,-0.8260899 -0.189298,-0.6273769,-1.0351 -0.177878,-0.6802909,-1.08276 -0.214254,-0.6642129,-1.04004 -0.161256,-0.6604429,-1.07782 -0.124653,-0.6590959,-1.07749 -0.108039,-0.624876,-1.03477 -0.052024,-0.654043,-0.9072679 -0.07875599,-0.5450549,-0.8260899 -0.111125,-0.6181819,-0.9507859 -0.184997,-0.6207029,-0.9477209 -0.120091,-0.5315399,-0.9014239 -0.09346699,-0.697089,-1.08694 -0.17714,-0.697089,-1.08694 -0.09536799,-0.6782579,-1.08225 -0.18433,-0.6892849,-0.864494 -0.228502,-0.6953909,-1.04422 -0.042748,-0.6953909,-1.04422 -0.054974,-0.6604409,-1.03953 -0.101027,-0.6892849,-0.864498 0.065548,-0.6953909,-1.04422 0.09233999,-0.6966199,-1.08576 0.102919,-0.6892849,-0.864498 0.192799,-0.6966189,-1.08576 0.09955399,-0.6772889,-1.08108 0.127676,-0.6576179,-1.07631 0.07295999,-0.6604409,-1.03931 0.120245,-0.5315399,-0.9014239 0.112616,-0.6181819,-0.9507859 0.109653,-0.6248769,-1.03477 0.187687,-0.6273769,-1.0351 0.17879,-0.6793749,-1.08158 0.162827,-0.6590009,-1.07665 0,0.194767,-1.45381 0.07399599,0.255722,-1.48352 0,0.286217,-1.51848 0.05122999,0.326388,-1.50415 0,0.213784,-1.35575 0.06501099,0.221538,-1.46405 0.182916,-0.6892849,-0.864494 0.171441,-0.5324879,-0.8961189 0.183557,-0.6207029,-0.9477209 0.215773,-0.6578469,-0.9026459 0.220422,-0.6953909,-1.04422 0.212566,-0.664214,-1.03982 0.067091,-0.158537,-0.6439149 0.130206,-0.193912,-0.21635 0.077416,0.007998999,-0.9647089 0.183964,-0.13444,-0.8866439 0.170979,-0.558332,-0.752044 0.194691,-0.546486,-0.8180879 0.09638299,0.183917,-1.07751 0.10255,0.289818,-1.15563 -0.067091,-0.158537,-0.6439149 -0.130206,-0.193912,-0.21635 -0.077416,0.007998999,-0.9647089 0,0.216114,-1.24481 0,0.218816,-1.31386 -0.183964,-0.13444,-0.8866439 -0.19522,-0.546486,-0.8180879 -0.171291,-0.558332,-0.752044 -0.10255,0.289818,-1.15563 -0.09638299,0.183918,-1.07751 -0.203324,-0.000186,-0.216355 0,0.46138,-0.216367 -0.132354,0.415323,-0.216366 0,0.483721,-1.42686 0,0.5050079,0.184315 -0.08362299,0.454487,-1.41915 -0.09797999,0.5992689,-1.04321 -0.150776,0.396113,-1.06888 0,0.668371,-1.24474 0,0.6600519,-1.12373 0.08362299,0.454486,-1.41915 0,0.6013789,-1.38424 0,0.6288509,-1.03919 0.09797999,0.599268,-1.04321 0.150776,0.396113,-1.06888 0,0.5882969,-0.9074389 0.132354,0.415323,-0.216366 0.203324,-0.000186,-0.216355 0.215676,-0.176166,-0.7959449 0.177974,0.25821,-0.971413 0.115772,0.5489519,-0.9114639 0.143614,0.490599,-0.781781 0.183334,-0.228667,-0.6926939 0.215131,0.109326,-0.8254889 0.11587,0.444022,-0.6659009 0.178124,0.029546,-0.6439199 0.160357,0.437455,-1.13712 0.226298,0.365261,-1.24324 0.105258,0.370388,-1.397 0.104248,0.6110719,-1.12795 0.08225499,0.262741,-1.36865 0.19287,0.403955,-1.34171 0.166988,0.246562,-1.2448 0.153069,0.277384,-1.32676 0.08922099,0.6455249,-1.24475 0.139971,0.697089,-1.33444 0.177051,0.54272,-1.24168 0.12615,0.5948719,-1.3384 0.041587,0.120377,0.5055919 0.06758299,0.28318,0.536543 0.104568,0.447773,0.443333 0,0.391487,0.530735 0.043936,0.380679,0.531314 0.149273,0.477514,0.293496 0.172353,-0.27574,0.220239 0.139752,-0.200327,0.44677 0.152251,-0.370463,0.291793 0.192606,-0.235087,0.341477 0.138378,-0.381752,0.461846 0.182423,-0.376028,0.382956 0.160847,0.216311,0.499174 0.116618,-0.015705,0.448978 0.110923,0.461129,0.163966 0.229189,0.09921499,0.308897 0.213423,-0.06605699,0.311063 0.176064,-0.153352,0.152359 0.170561,0.060886,0.085922 -0.215676,-0.176166,-0.7959449 -0.177974,0.25821,-0.971413 -0.115772,0.5489519,-0.9114639 -0.143614,0.490599,-0.781781 0,0.488737,-0.6659029 0,0.5342709,-0.777755 -0.183334,-0.228667,-0.6926939 -0.215131,0.109326,-0.8254889 -0.178124,0.029546,-0.6439199 -0.11587,0.444022,-0.6659009 -0.160357,0.437455,-1.13712 -0.226298,0.365261,-1.24324 -0.105258,0.370388,-1.397 -0.104248,0.6110719,-1.12795 -0.166988,0.246562,-1.2448 -0.19287,0.403955,-1.34171 -0.08225499,0.262741,-1.36865 -0.153069,0.277384,-1.32676 -0.08922099,0.6455249,-1.24475 -0.139971,0.697089,-1.33444 -0.12615,0.5948719,-1.3384 -0.177051,0.54272,-1.24168 -0.041587,0.120377,0.5055919 0,0.228662,0.5394669 0,0.519446,0.291789 -0.149273,0.477514,0.293496 -0.172353,-0.27574,0.220239 -0.139752,-0.200327,0.44677 -0.138378,-0.381752,0.461846 -0.192606,-0.235087,0.341477 -0.152251,-0.370463,0.291794 -0.182423,-0.376028,0.382957 -0.116618,-0.015705,0.448978 -0.160847,0.216311,0.499174 0,0.469387,0.442753 -0.104568,0.447772,0.443333 -0.06758299,0.28318,0.536543 -0.043936,0.380679,0.531314 -0.110923,0.461129,0.163966 -0.229189,0.09921499,0.308897 -0.170561,0.060886,0.085922 -0.176064,-0.153352,0.152359 -0.213423,-0.06605699,0.311063";
            string textureCoords = "0.19086,-0.544059 0.272086,-0.355992 0.077337,-0.522561 0.187013,-0.516511 0.180133,-0.467245 0.17937,-0.461784 0.265206,-0.306726 0.004406,-0.493426 0.264443,-0.301265 0.004719,-0.489377 0.077765,-0.579757 0.07738,-0.528268 0.004406,-0.493426 0.001581,-0.529958 0.265206,-0.306726 0.272086,-0.355992 0.275933,-0.38354 0.180133,-0.467245 0.187013,-0.516511 0.077979,-0.608548 0,-0.550385 0.077765,-0.579757 0.001581,-0.529958 0.07738,-0.528268 0.764787,-0.600058 0.712026,-0.640601 0.88285,-0.350956 0.997293,-0.254425 0.571239,-0.666781 0.466839,-0.585368 0.466839,-0.585368 0.465868,-0.611086 0.395923,-0.575438 0.395452,-0.580559 0.33336,-0.419449 0.395923,-0.575438 0.355184,-0.527123 0.355184,-0.527123 0.355215,-0.645708 0.389458,-0.670719 0.400009,-0.577639 0.450862,-0.619968 0.428283,-0.698636 0.34975,-0.774111 0.450862,-0.619968 0.428283,-0.698636 0.400009,-0.577639 0.34975,-0.774111 0.355215,-0.645708 0.375498,-0.770684 0.389458,-0.670719 0.375498,-0.770684 0.38032,-0.814463 0.385866,-0.993469 0.399884,-0.804614 0.405685,-0.932268 0.387276,-0.940921 0.415286,-0.993271 0.385034,-0.993466 0.396878,-0.993421 0.421811,-0.993083 0.397606,-0.993432 0.432642,-0.932004 0.422107,-0.942626 0.432889,-0.932775 0.405989,-0.93304 0.388004,-0.942997 0.429913,-0.99306 0.404586,-0.766082 0.399436,-0.803843 0.429321,-0.99305 0.415226,-0.940741 0.450708,-0.993185 0.442901,-0.963505 0.454028,-0.958674 0.440448,-0.993006 0.461835,-0.99316 0.454276,-0.95911 0.440696,-0.99301 0.462082,-0.993162 0.443493,-0.964678 0.4513,-0.99319 0.363002,-0.8218 0.375432,-0.949709 0.376264,-0.950333 0.379246,-0.812387 0.361774,-0.821176 0.763676,-0.594291 0.794303,-0.593952 0.79722,-0.971853 0.795071,-0.881937 0.988489,-0.316579 0.995281,-0.265891 0.982078,-0.341098 0.728091,-0.664021 0.363002,-0.8218 0.38032,-0.814463 0.376264,-0.950333 0.385866,-0.993469 0.399884,-0.804614 0.432642,-0.932004 0.388004,-0.942997 0.405989,-0.93304 0.432889,-0.932775 0.422107,-0.942626 0.404586,-0.766082 0.450708,-0.993185 0.454028,-0.958674 0.442901,-0.963505 0.461835,-0.99316 0.454276,-0.95911 0.429913,-0.99306 0.440696,-0.99301 0.4513,-0.99319 0.443493,-0.964678 0.462082,-0.993162 0.399436,-0.803843 0.405685,-0.932268 0.415287,-0.993271 0.421811,-0.993083 0.397606,-0.993432 0.396878,-0.993421 0.440448,-0.993006 0.429321,-0.99305 0.415226,-0.940741 0.387276,-0.940921 0.375432,-0.949709 0.385034,-0.993466 0.379246,-0.812387 0.361774,-0.821176 0.747633,-0.900476 0.747633,-0.900476 0.76091,-0.629293 0.728091,-0.664021 0.76091,-0.629293 0.794303,-0.593952 0.763676,-0.594291 0.818767,-0.503206 0.855912,-0.385869 0.798742,-0.969124 0.772012,-0.890951 0.840834,-0.949998 0.856528,-0.987951 0.842461,-0.976419 0.854901,-0.973715 0.854792,-0.972749 0.840726,-0.948204 0.798742,-0.969124 0.772012,-0.890951 0.813072,-0.943402 0.812062,-0.945211 0.796818,-0.881257 0.857904,-1 0.857904,-1 0.85636,-0.986493 0.784658,-0.994402 0.843837,-0.998782 0.843837,-0.998782 0.842293,-0.973714 0.784659,-0.994402 0.843837,-0.998782 0.857515,-0.999664 0.784659,-0.994402 0.857515,-0.999663 0.855974,-0.985798 0.854404,-0.971689 0.842221,-0.973714 0.796818,-0.881257 0.813072,-0.943402 0.840726,-0.948205 0.840834,-0.949998 0.856139,-0.987294 0.854516,-0.972681 0.978706,-0.3603 0.988489,-0.316579 1,-0.294706 0.995281,-0.265892 0.946417,-0.346659 0.982078,-0.341098 0.784658,-0.994402 0.795071,-0.881937 0.812062,-0.945211 0.79722,-0.971853 0.843837,-0.998782 0.842388,-0.97642 0.712026,-0.613714 0.571239,-0.639087 0.817656,-0.494263 0.791951,-0.59643 0.747631,-0.900474 0.769377,-0.891977 0.854799,-0.368082 0.880522,-0.292123 0.712026,-0.613714 0.571239,-0.639087 0.817656,-0.494263 0.909887,-0.344988 0.932623,-0.34305 0.791951,-0.59643 0.769377,-0.891977 0.747631,-0.900474 0.880522,-0.292123 0.854799,-0.368081 0.571241,-0.500134 0.571245,-0.169067 0.571244,-0.202102 0.969832,-0.153042 0.439309,-0.137774 0.967293,-0.174011 0.843505,-0.070163 0.851957,-0.215881 0.909864,-0.020599 0.870018,-0.026565 0.967293,-0.174011 0.955798,-0.06865 0.842181,-0.048945 0.843505,-0.070164 0.851957,-0.215881 0.798798,-0.078033 0.571244,-0.202102 0.571241,-0.500134 0.762086,-0.626358 0.819864,-0.314794 0.800124,-0.106254 0.757422,-0.148109 0.728088,-0.664016 0.771814,-0.421584 0.719266,-0.181517 0.712028,-0.478808 0.874427,-0.186227 0.90937,-0.23801 0.959999,-0.234332 0.871408,-0.061697 0.950664,-0.311544 0.941794,-0.210256 0.909884,-0.323149 0.936871,-0.301041 0.909867,-0.036985 0.9394,0 0.908856,-0.110724 0.940704,-0.073317 0.33352,-0.413657 0.323329,-0.296884 0.354021,-0.178827 0.325241,-0.219199 0.325051,-0.226951 0.403359,-0.157494 0.42748,-0.69778 0.352889,-0.643688 0.403919,-0.765721 0.38756,-0.668621 0.347925,-0.773819 0.373902,-0.769713 0.335634,-0.344847 0.352162,-0.511265 0.44601,-0.169247 0.398287,-0.428836 0.397574,-0.547381 0.449832,-0.609995 0.471708,-0.456328 0.762086,-0.626358 0.819864,-0.314794 0.800124,-0.106254 0.757422,-0.148109 0.719266,-0.149444 0.756097,-0.116784 0.728088,-0.664016 0.771814,-0.421584 0.712028,-0.478808 0.719266,-0.181517 0.874427,-0.186227 0.90937,-0.23801 0.959999,-0.234332 0.871408,-0.061697 0.909884,-0.323149 0.941794,-0.210256 0.950664,-0.311544 0.936871,-0.301041 0.909867,-0.036985 0.9394,0 0.940704,-0.073317 0.908856,-0.110724 0.33352,-0.413657 0.322366,-0.335988 0.403921,-0.127418 0.403359,-0.157494 0.42748,-0.69778 0.352889,-0.643688 0.347925,-0.773819 0.38756,-0.668621 0.403919,-0.765721 0.373901,-0.769713 0.352162,-0.511265 0.335634,-0.344847 0.354212,-0.163323 0.354021,-0.178827 0.323329,-0.296884 0.325051,-0.226951 0.44601,-0.169247 0.398287,-0.428836 0.471708,-0.456328 0.449832,-0.609995 0.397574,-0.547381";
            string triangleIndices = "300,301,302 299,300,302 298,300,299 295,296,297 294,295,297 299,292,293 302,292,299 289,290,291 288,289,291 287,289,288 286,290,289 287,292,302 289,287,302 286,302,301 289,302,286 284,298,285 294,284,285 293,282,283 296,293,283 295,294,285 292,282,293 295,293,296 293,295,285 285,299,293 298,299,285 279,280,281 278,280,279 281,278,279 275,276,277 274,275,277 273,278,281 272,276,275 271,275,274 280,272,275 270,271,274 270,273,281 281,280,275 271,270,281 275,271,281 267,268,269 266,268,267 263,264,265 262,263,265 269,264,263 261,263,262 260,266,267 267,269,263 261,260,267 263,261,267 257,258,259 256,257,259 255,256,259 256,253,254 257,256,254 250,251,252 249,250,252 248,251,250 247,250,249 247,258,257 248,257,254 250,257,248 250,247,257 246,255,284 294,246,284 243,244,245 294,244,243 253,245,242 241,253,242 246,294,243 254,253,241 243,245,253 253,246,243 246,253,256 255,246,256 238,239,240 237,238,240 239,238,237 234,235,236 233,234,236 232,239,237 231,234,233 240,234,231 230,235,234 229,235,230 229,239,232 239,234,240 230,239,229 230,234,239 226,227,228 225,226,228 224,265,264 227,224,264 223,265,224 222,223,224 221,226,225 226,224,227 222,226,221 224,226,222 219,220,228 218,265,223 217,223,222 216,223,217 229,216,217 216,215,218 232,215,216 232,216,229 223,216,218 213,214,240 231,213,240 237,211,212 211,240,214 237,240,211 212,215,232 237,212,232 209,270,210 209,215,273 273,270,209 208,272,280 278,212,211 214,208,280 280,278,211 212,273,215 278,273,212 207,298,284 207,284,255 206,208,214 213,206,214 218,262,265 209,262,218 215,209,218 210,262,209 261,262,210 214,280,211 297,244,294 219,207,255 205,298,207 204,207,219 204,205,207 227,204,219 228,227,219 205,268,203 269,268,205 269,205,204 210,201,202 261,210,202 199,200,260 198,199,260 270,201,210 197,277,276 274,197,196 201,274,196 277,197,274 270,274,201 195,261,202 266,260,200 198,261,195 203,193,194 300,203,194 268,193,203 260,261,198 229,217,192 191,192,217 222,191,217 221,189,190 188,221,190 197,233,236 235,196,197 192,196,235 236,235,197 229,192,235 187,191,222 225,189,221 188,187,222 220,259,186 185,220,186 228,220,185 221,188,222 182,183,184 181,182,184 180,182,181 190,179,182 190,182,180 233,177,178 231,233,178 174,175,176 173,178,177 175,178,173 174,213,231 176,213,174 174,178,175 178,174,231 170,171,172 169,170,172 184,171,170 170,168,181 169,168,170 170,181,184 167,181,168 180,181,167 165,166,169 164,166,165 172,165,169 163,184,183 171,184,163 179,189,162 163,161,164 165,172,171 164,165,171 163,164,171 183,161,163 160,161,183 190,189,179 179,160,183 162,160,179 179,183,182 157,158,159 166,160,162 157,155,156 159,155,157 152,153,154 157,154,158 152,154,157 149,150,151 148,149,151 147,149,148 145,146,158 154,145,158 145,144,146 142,153,143 141,142,143 156,143,153 144,142,141 141,150,149 146,144,141 142,154,153 145,154,142 144,145,142 149,146,141 146,147,158 149,147,146 159,158,147 168,139,140 169,139,168 166,139,169 167,168,140 162,139,166 164,161,160 166,164,160 187,137,138 191,187,138 152,157,156 152,156,153 136,137,187 135,136,187 187,188,135 135,188,180 134,185,136 135,134,136 148,132,133 131,148,133 151,132,148 130,139,162 140,139,130 159,131,200 155,159,200 147,131,159 148,131,147 129,252,251 128,249,252 128,252,129 125,126,127 125,123,124 121,122,123 121,127,120 119,121,120 118,121,119 122,121,118 125,121,123 117,125,124 116,125,117 128,125,116 125,127,121 126,125,128 113,114,115 112,113,115 111,113,112 110,115,114 109,115,110 115,109,122 122,112,115 106,107,108 109,107,106 106,124,123 108,124,106 110,107,109 123,122,109 109,106,123 116,249,128 105,249,116 103,110,104 103,101,102 104,101,103 103,100,107 110,103,107 100,103,102 119,111,112 114,104,110 108,107,100 112,122,118 118,119,112 117,100,102 124,100,117 100,124,108 99,105,116 120,101,104 111,104,114 111,119,120 111,114,113 104,111,120 98,120,127 97,120,98 101,120,97 101,95,96 97,95,101 102,101,96 126,97,98 126,98,127 129,95,97 99,102,96 117,102,99 126,129,97 116,117,99 128,129,126 185,134,94 94,228,185 228,94,225 140,130,94 130,162,189 130,225,94 225,130,189 167,140,134 134,140,94 134,135,167 167,135,180 190,180,188 199,155,200 93,177,276 272,93,276 91,92,175 173,177,93 175,173,93 91,272,208 92,91,208 91,175,93 93,272,91 90,151,150 90,89,199 89,90,150 141,143,150 143,89,150 156,89,143 89,155,199 155,89,156 198,88,90 151,90,88 199,198,90 138,137,195 202,138,195 87,195,137 88,195,87 88,198,195 87,193,132 88,87,132 86,288,291 85,86,291 85,291,290 86,83,84 82,86,84 85,83,86 79,80,81 78,79,81 77,80,79 76,77,79 75,79,78 76,79,75 72,73,74 76,72,74 77,76,74 72,70,71 73,72,71 76,75,70 72,76,70 69,85,290 68,69,290 67,78,81 64,65,66 63,64,66 77,64,63 64,74,62 77,74,64 62,65,64 80,77,63 59,60,61 58,59,61 57,60,59 74,73,62 56,59,58 55,65,62 78,60,57 75,78,57 67,60,78 71,55,62 56,55,71 73,71,62 56,70,59 71,70,56 59,75,57 70,75,59 54,69,68 81,80,67 61,63,66 67,61,60 63,61,67 80,63,67 61,84,53 58,61,53 66,84,61 82,84,66 52,82,66 65,52,66 83,56,58 83,58,53 84,83,53 54,52,65 55,54,65 69,54,55 56,69,55 85,69,56 56,83,85 133,132,193 193,268,133 266,133,268 266,131,133 200,131,266 132,151,88 51,105,99 49,68,50 47,48,287 288,47,287 82,47,288 49,52,54 68,49,54 82,52,47 49,48,47 50,48,49 49,47,52 96,51,99 45,46,50 44,46,45 44,286,301 45,286,44 45,290,286 68,290,45 68,45,50 86,82,288 95,251,43 96,95,43 51,96,43 129,251,95 40,41,42 39,40,42 247,42,41 249,42,247 105,42,249 105,39,42 258,247,41 51,38,39 43,38,51 105,51,39 248,38,43 251,248,43 38,248,254 37,38,254 37,40,39 38,37,39 36,48,50 46,36,50 35,46,44 282,36,46 33,34,282 241,34,33 32,241,33 35,282,46 33,282,35 31,32,33 35,31,33 30,31,35 292,48,36 44,301,30 30,301,300 287,48,292 44,30,35 32,41,40 28,29,31 241,40,37 32,40,241 32,31,29 28,31,30 29,259,258 258,41,29 29,41,32 186,29,28 29,186,259 254,241,37 282,292,36 27,176,175 92,27,175 27,92,206 176,27,206 197,177,233 177,197,276 208,206,92 213,176,206 26,196,192 26,201,196 191,26,192 138,26,191 26,138,202 202,201,26 24,185,25 193,24,25 136,185,24 87,24,193 24,87,137 137,136,24 194,30,300 194,28,30 25,28,194 21,22,23 20,22,21 19,20,21 19,21,18 21,17,18 23,17,21 15,16,283 283,241,242 14,242,245 15,242,14 283,242,15 13,22,20 19,13,20 10,11,12 11,9,12 8,245,244 14,245,8 12,7,22 12,22,13 9,7,12 12,13,10 10,13,19 6,244,297 8,6,17 5,8,17 14,5,4 8,5,14 244,6,8 3,15,4 4,15,14 3,16,15 4,2,11 5,2,4 3,4,11 16,296,283 1,296,16 1,297,296 6,297,1 2,7,9 23,7,2 22,7,23 0,18,1 16,0,1 17,6,1 17,1,18 3,0,16 34,241,283 282,34,283 11,2,9 23,2,5 23,5,17 10,3,11 18,0,19 0,3,10 19,0,10 25,185,186 28,25,186 193,25,194 264,269,204 227,264,204 255,220,219 259,220,255 298,205,203 300,298,203";

            //Get all of the positions of the polygons
            string[] posArr = positions.Split(' ');
            Point3DCollection ps = new Point3DCollection();
            foreach (string p in posArr)
            {
                string[] vals = p.Split(',');
                ps.Add(new Point3D(double.Parse(vals[0]), double.Parse(vals[1]), double.Parse(vals[2])));
            }

            //Get the texture co-ords
            string[] texArr = textureCoords.Split(' ');
            Kit3D.Windows.Media.PointCollection pc = new Kit3D.Windows.Media.PointCollection();
            foreach (string t in texArr)
            {
                string[] vals = t.Split(',');
                double u = double.Parse(vals[0]);
                double v = double.Parse(vals[1]);
                pc.Add(new Point(u, v));
            }

            //Get the triangle indices
            string[] triArr = triangleIndices.Split(' ');
            Int32Collection ic = new Int32Collection();
            foreach (string t in triArr)
            {
                string[] vals = t.Split(',');
                ic.Add(int.Parse(vals[0]));
                ic.Add(int.Parse(vals[1]));
                ic.Add(int.Parse(vals[2]));
            }

            //The mesh is an abstraction of all of the polygons and texture information
            //for a model
            MeshGeometry3D mesh = new MeshGeometry3D();
            mesh.Positions = ps;
            mesh.TriangleIndices = ic;
            mesh.TextureCoordinates = pc;

            //A model consists of mesh information and material information
            GeometryModel3D model = new GeometryModel3D();

            //Notice here the normal Silverlight brush is wrapped in a Kit3DBrush, this is because
            //there are certain properties of the brushes that we cannot get programatically so they
            //have to be passed in by the user when creating the brush.
            model.Material = new DiffuseMaterial(new Kit3DBrush(new SolidColorBrush(fillColor)));
            model.Geometry = mesh;

            //This will push all of the polygons away from eachother slightly so the polygon seams
            //are visible (for demo purposes), normally you will set this to be a positive value
            model.SeamSmoothing = -1;

            //The model visual is a type that knows how to draw the Model
            ModelVisual3D modelVisual = new ModelVisual3D();
            modelVisual.Content = model;

            return modelVisual;
        }
    }
}
